#coding=utf-8
import codecs
import collections
import json
import os, re
import os.path
import sys
import traceback
import glob
import htmlmin

from bs4 import BeautifulSoup
from readmdict import MDX, MDD

mdx_file="e:\\a\\CollinsEDT.mdx"

def parse_extra(explain):
    result = ""
    soup = BeautifulSoup(explain, "html.parser")
    #print(soup.prettify())
    ggs=soup.select('span[class="gramGrp"] > span[class="pos"]')
    #for g in ggs:
    #    #tf.write("{0!s}|{1!s}\n".format(word, g.get_text()))
    result = ",".join(g.get_text() for g in ggs) + "|"

    ggs=soup.select('span[class*="inflected_forms"] > span[class="orth"]')
    #for g in ggs:
    #    #tf.write("{0!s}|{1!s}\n".format(word, g.get_text()))
    result = result + ",".join(g.get_text() for g in ggs) + "|"

    return result

def parse_mdx(mdx_file):
    if not os.path.exists(mdx_file):
        print("Please specify a valid MDX/MDD file")

    base, ext = os.path.splitext(mdx_file)

    # write out glos
    only_test=True
    if only_test:
        output_fname = ''.join(["e:\\test", os.path.extsep, 'txt.pos'])
        tf = open(output_fname, 'a+',encoding='utf8')
    else:
        output_fname = ''.join([base, os.path.extsep, 'txt.pos'])
        tf = open(output_fname, 'w',encoding='utf8')

    # read mdx file
    try:
        mdx = MDX(mdx_file, "", False, None)
        if type(mdx_file) is str:
            bfname = mdx_file.encode('utf-8')
        else:
            bfname = mdx_file
        print('======== %s ========' % bfname)
        print('  Number of Entries : %d' % len(mdx))

        try:
            tf.write("=========== "+mdx_file+"\n")
        except:
            print(traceback.format_exc())

        #for key, value in mdx.header.items():
        #    print('  %s : %s' % (key, value))

        for key, value in mdx.items():
            try:
                # bring||brought|brought|
                word = key.decode("utf-8", "ignore").strip()
                explain = value.decode("utf-8", "ignore").strip()
    
                if only_test is False:
                    tf.write("{}|{}\n".format(word, parse_extra(explain)))
                elif only_test is False and False:
                    #tf.write(word + " " + str(type(word)) +"\n")
                    if "bring" == word :
                        soup = BeautifulSoup(explain, "html.parser")
                        #print(soup.prettify())
                        ggs=soup.select('span[class="gramGrp"] > span[class="pos"]')
                        for g in ggs:
                            tf.write("{0!s}|{1!s}\n".format(word, g.get_text()))
    
                        ggs=soup.select('span[class*="inflected_forms"] > span[class="orth"]')
                        for g in ggs:
                            tf.write("{0!s}|{1!s}\n".format(word, g.get_text()))
    
                        #tf.write("{0!s}|{1!s}\n".format(word, explain))
                        #print('  %s : %s' % (key, value))
                else:
                    tf.write(word+"\n")
                    #if "bring" == word :
                    #    tf.write("{0!s}|{1!s}\n".format(word, htmlmin.minify(explain)))
    
            except:
                print(traceback.format_exc())
    except:
        return
        
    tf.close()

#====================================
files=glob.glob("G:\\dict\\*.mdx")
for f in files:
    parse_mdx(f)


